<?php
namespace Admin\Model;

use Common\Model\CommonModel;

class RechargeRebateModel extends CommonModel {

    /**
     * 获取列表
     */
    public function get_list($where = array(),$from,$to){
        $list = M("RechargeRebate")->field("*")->where($where)
            ->order("start_price")
            ->limit($from ,$to)
            ->select();
        return $list;
    }

    /**
     * 获取单个信息
     */
    public function get_one($where){
        $item = M("RechargeRebate")->field("*")->where($where)
            ->find();
        return $item;
    }

    /**
     * 更新
     */
    public function update_data($data,$id){
        $data = array_merge($data,['update_time'=>time()]);
        $result = M("RechargeRebate")->where(['id'=>$id])->save($data);
        return $result;
    }

    /**
     * 新增
     */
    public function add_data($insert){
        $insert = array_merge($insert,['create_time'=>time(),'update_time'=>time()]);
        $result =  M("RechargeRebate")->add($insert);
        return $result;
    }

    /**
     * 检测金额
     */
    public function check_price($start_price,$end_price,$id = ''){
        $where = "((start_price<=".$start_price." AND end_price>=".$start_price.") OR (start_price<=".$end_price." AND end_price>=".$end_price.")  OR (start_price>".$start_price." AND end_price<".$end_price."))";
        if(!empty($id)){
            $where .= " AND id!=".$id;
        }
        $result = M("RechargeRebate")->field("*")->where($where)
            ->find();
       return $result;
    }

    /**
     * 获取该充值金额的返利金额
     */
    public function get_rebate_price($recharge_price = ''){
        $rebate_price = 0;
        if(!empty($recharge_price)){
            if(session('ADMIN_ID') == 306){
                $result['rebate_percent'] = 4;
            }elseif(session('ADMIN_ID') == 176){
                $result['rebate_percent'] = 3;
            }else{
                $where = " start_price<=".$recharge_price." AND ".$recharge_price."<=end_price";
                $result = M("RechargeRebate")->field("*")->where($where)->order("start_price")
                    ->find();
            }

            if($result){
                $rebate_percent = $result['rebate_percent'];
                $rebate_price = round($recharge_price*$rebate_percent/100,2);
            }
        }
        return $rebate_price;
    }
}